<?php
	/**
	 * Страница рассеча динамики товаров.
	 * 
	 * @author Костин Алексей Васильевич aka Volt(220)
	 * @copyright Copyright (c) 2010, Костин Алексей Васильевич
	 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public License
	 * @version 1.0
	 * @package HomeMoney
	 */

	/**
	 * Подключение настроек. 
	 */
	require_once("../ini.php");

	$DBCon=SQLDBFactory::getDB();

	$curTab=0;

	$sql="SELECT min(date) as С, max(date) as По FROM 
		(select date from prihod union select date from buys) as tab";
	$rez=$DBCon->select($sql, null, null, 1);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Промежуток времени";
	$curTab++;

	$sql="SELECT item as Товар, cat as Категория, kol as Количество, sum as `Всего`, min as `Минимальная цена`, sred as `Средняя цена`, max as `Максимальная цена`, round(sum-kol*min) as `Потеряная выгода`
		from goods 
		LEFT OUTER JOIN categ ON goods.id_cat = categ.id
		LEFT OUTER JOIN (select id_good,  round(sum(col),3) as kol, round(sum(sum),2) as sum from basket group by id_good) as t1 on t1.id_good=goods.id
		LEFT OUTER JOIN (select id_good, max(price) as max from basket group by id_good) as t2 on t2.id_good=goods.id
		LEFT OUTER JOIN (select id_good, min(price) as min from basket group by id_good) as t3 on t3.id_good=goods.id
		LEFT OUTER JOIN (select id_good, round(avg(price),2) as sred from basket group by id_good) as t4 on t4.id_good=goods.id
		order by item";
	$rez=$DBCon->select($sql);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Динамика товаров";
	$tabs[$curTab]->sumRows(8+128);
	$curTab++;

	$bodyTpl=new Template(DOCROOT."/Templates/tablesPrint.tpl");
	$bodyTpl->tables=$tabs;

	$pageT="Динамика товаров";
	include (DOCROOT."/pages.php"); 
?>